<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="报警设备" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入报警设备名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-device:log:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="logList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报警规则" align="center" prop="ruleName" />
      <!-- <el-table-column label="报警设备id" align="center" prop="deviceId" /> -->
      <el-table-column label="报警设备名称" align="center" prop="deviceName" />
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <div :style="scope.row.stateStyle">{{ scope.row.stateResult }}</div>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="actionResult" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Log">
onMounted(() => {
  getList();
});
import { listLog, getLog, delLog, addLog, updateLog } from "@/api/alarm/log";
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();

const logList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceName: null,
    state: null,
  },
  rules: {
    ruleId: [{ required: true, message: "规则id不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询报警记录列表 */
function getList() {
  loading.value = true;
  listLog(queryParams.value).then((response) => {
    logList.value = response.rows;
    total.value = response.total;
    loading.value = false;

    logList.value.forEach((item1) => {
      if (item1.state == 0) {
        item1.stateResult = "未处理";
        item1.stateStyle = {
          color: "red",
        };
      } else if (item1.state == 1) {
        item1.stateResult = "已处理";
        item1.stateStyle = {
          color: "green",
        };
      }
    });
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/log/export",
    {
      ...queryParams.value,
    },
    `log_${new Date().getTime()}.xlsx`
  );
}
</script>
